package com.sservice.service.service.impl;

import com.sservice.common.Constants;
import com.sservice.model.dao.ISaleCashApplyDao;
import com.sservice.model.dao.ISalePersonAccountDao;
import com.sservice.model.dao.ISecretaryDao;
import com.sservice.model.entity.SaleCashApply;
import com.sservice.model.entity.SalePersonAccount;
import com.sservice.model.entity.Secretary;
import com.sservice.model.vo.SecretaryVo;
import com.sservice.service.service.ISecretaryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
@Transactional
public class SecretaryServiceImpl implements ISecretaryService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ISecretaryDao userDao;

    @Autowired
    private ISalePersonAccountDao spaDao;
    @Autowired
    private ISaleCashApplyDao scaDao;

    @Override
    public List<Secretary> queryByPage(SecretaryVo user) {
        return userDao.queryByPage(user);
    }

    @Override
    public Secretary queryUserByPhone(String phone) {

        return null;
    }


    @Override
    public int deleteByPrimaryKey(Integer id) {
        return userDao.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Secretary record) {
        return userDao.insert(record);
    }

    @Override
    public int insertSelective(Secretary record) {
        return userDao.insertSelective(record);
    }

    @Override
    public Secretary selectByPrimaryKey(Integer id) {
        return userDao.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Secretary record) {
        return userDao.updateByPrimaryKeySelective(record);
    }


    @Override
    public int queryCount(SecretaryVo user) {
        return userDao.queryCount(user);
    }

    @Override
    public Object confirm(Secretary record) {
        if (record.getId() != null && record.getCost() != null) {
            Secretary s = userDao.selectByPrimaryKey(record.getId());
            SalePersonAccount spa = spaDao.selectBySaleID(s.getSaleId());
            if (record.getCost() > spa.getCashCanWithDrawTotal().doubleValue()) {
                return "参数非法";
            } else {
                spa.setCashCanWithDrawTotal(spa.getCashCanWithDrawTotal().subtract(BigDecimal.valueOf(record.getCost())));
                //todo:佣金消费记录
                spa.setCashReceivedTotal(spa.getCashReceivedTotal().add(BigDecimal.valueOf(record.getCost())));
                SaleCashApply sca = new SaleCashApply();
                sca.setNo(Constants.generateServiceNO("T"));
//                0：飞机票，1：火车票,2：酒店预定，3：话费充值
                if ("0".equals(s.getType())) {
                    sca.setType("订购飞机票");
                }
                if ("1".equals(s.getType())) {
                    sca.setType("订购火车票");
                }
                if ("2".equals(s.getType())) {
                    sca.setType("酒店预定");
                }
                if ("3".equals(s.getType())) {
                    sca.setType("话费充值");
                }
                if ("4".equals(s.getType())) {
                    sca.setType("流量充值");
                }
                sca.setApplyCount(s.getCost());
                sca.setSaleId(s.getSaleId());
                sca.setStatus("3");///已打款
                scaDao.insertSelective(sca);
                spaDao.updateByPrimaryKeySelective(spa); // 实际花费
            }
        }
        record.setStatus("3");
        return userDao.updateByPrimaryKeySelective(record);
    }
}
